package com.atguigu.tingshu.album.config;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.ExecutorService;

@Configuration
public class BloomFilterConfig {
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ExecutorService executorService;
    @Resource
    private AlbumInfoMapper albumInfoMapper;
    @Resource
    private TrackInfoMapper trackInfoMapper;
    @Resource
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Bean
    public RBloomFilter<String> rBloomFilter() {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        bloomFilter.tryInit(100000000, 0.03);
        //每页查询条数
        long pageSize = 10000;
        ///专辑
        //获取总记录条数
        long albumCount = this.albumInfoMapper.selectCount(null);
        //计算总页数
        long albumTotalNum = albumCount % pageSize == 0 ? albumCount / pageSize : albumCount / pageSize + 1;
        for (long i = 0; i < albumTotalNum; i++) {
            long finalI = i;
            executorService.execute(() -> {
                IPage<AlbumListVo> userAlbumPage = this.albumInfoMapper.findUserAlbumPage(new Page<>(finalI + 1, pageSize), new AlbumInfoQuery());
                List<AlbumListVo> albumListVos = userAlbumPage.getRecords();
                albumListVos.forEach(albumListVo -> bloomFilter.add(RedisConstant.ALBUM_INFO_PREFIX + albumListVo.getAlbumId()));
            });
        }
        ///声音
        //获取总记录条数
        long trackCount = this.trackInfoMapper.selectCount(null);
        //计算总页数
        long trackTotalNum = trackCount % pageSize == 0 ? trackCount / pageSize : trackCount / pageSize + 1;
        for (long i = 0; i < trackTotalNum; i++) {
            long finalI = i;
            executorService.execute(() -> {
                Page<TrackListVo> userTrackPage = this.trackInfoMapper.findUserTrackPage(new Page<>(finalI + 1, pageSize), new TrackInfoQuery());
                List<TrackListVo> trackListVos = userTrackPage.getRecords();
                trackListVos.forEach(trackListVo -> bloomFilter.add(RedisConstant.TRACK_INFO_PREFIX + trackListVo.getTrackId()));
            });
        }
        ///分类
        List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(null);
        baseCategoryViews.forEach(baseCategoryView -> bloomFilter.add(RedisConstant.CATEGORY_INFO_PREFIX + baseCategoryView.getId()));

        return bloomFilter;
    }
}
